package com.spzx.user.controller;

;
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;

import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.vo.ProductSkuVo;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.domain.*;
import com.spzx.user.service.IUserBrowseHistoryService;
import com.spzx.user.service.IUserCollectService;
import com.spzx.user.service.IUserInfoService;

import com.spzx.user.vo.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-11-18
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserCollectService userCollectService;

    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody UserInfo userInfo) {
        userInfoService.register(userInfo);
        return R.ok();
    }

    @Operation(summary = "根据用户名获取用户信息")
    @InnerAuth
    @GetMapping("/info/{username}")
    public R<UserInfo> getUserInfo(@PathVariable("username") String username) {
        UserInfo userInfo = userInfoService.selectUserByUserName(username);
        return R.ok(userInfo);
    }

    @Operation(summary = "更新用户登录信息")
    @InnerAuth
    @PutMapping("/updateUserLogin")
    public R<Boolean> updateUserLogin(@RequestBody UpdateUserLogin updateUserLogin) {
        return R.ok(userInfoService.updateUserLogin(updateUserLogin));
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/getLoginUserInfo")
    public AjaxResult getLoginUserInfo(HttpServletRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return success(userInfoVo);
    }
    @Operation(summary = "判断当前商品是否收藏")
    @Parameter(name = "skuId", description = "商品skuId", required = true)
    @GetMapping("/isCollect/{skuId}")
    public AjaxResult isCollect(@PathVariable("skuId") Long skuId) {
        userBrowseHistoryService.saveUserBrowseHistory(skuId);
        boolean isCollect = userCollectService.isCollect(skuId);
        return success(isCollect);
    }


    @Operation(summary = "收藏当前商品")
    @Parameter(name = "skuId", description = "商品skuId", required = true)
    @GetMapping("/collect/{skuId}")
    public AjaxResult collect(@PathVariable("skuId")  Long skuId) {
        userCollectService.collect(skuId);
        return success();
    }
    @Operation(summary = "取消收藏当前商品")
    @Parameter(name = "skuId", description = "商品skuId", required = true)
    @GetMapping("/cancelCollect/{skuId}")
    public AjaxResult cancelCollect(@PathVariable("skuId") Long skuId) {

        userCollectService.cancelCollect(skuId);
        return success();
    }

    @Autowired
    private IUserBrowseHistoryService userBrowseHistoryService;


    @Operation(summary = "获取当前用户浏览列表")
    @GetMapping("/userBrowseHistoryList/{pageNum}/{pageSize}")
    public TableDataInfo userBrowseHistoryList(@PathVariable("pageNum") Integer pageNum,
                                            @PathVariable("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSkuVo> list = userBrowseHistoryService.getUserBrowseHistoryList();
        return getDataTable(list);
    }


    @Operation(summary = "查看我的收藏")
    @GetMapping("/userCollectList/{pageNum}/{pageSize}")
    public TableDataInfo userCollectList(@PathVariable("pageNum") Integer pageNum,
                                         @PathVariable("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSkuVo> list = userCollectService.getUserCollectList();
        return getDataTable(list);
    }
}
